﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ORMAC.CodeGen;
using ORMAC.UI.WPF.ViewModel;
using System.IO;
using ORMAC.Core;
using ORMAC.UI.WPF.Extensions;
using ORMAC.UI.WPF.Business;
using ORMAC.UI.WPF.Views;
using System.Windows;
namespace ORMAC.UI.WPF
{
    public class DatabaseImportWizard
    {
        private ORMACDBConfiguration config = null;
        private int wizardStep = 0;
        private List<IWizardForm> forms;
        //private ICodeGenerator generator=new ORMAC.CodeGen.CodeGenerator();
        //private List<DBObjectInfo> selectedObjects =new List<DBObjectInfo>();
        //private string cnnString = string.Empty;
        //private string dbName = string.Empty;

        private void Init()
        {

            //create a db configuration if no one exists
            if (ORMACConfiguration.Instance.DBConfiguration.Count == 0)
            {
                config = new ORMACDBConfiguration();
                ORMACConfiguration.Instance.DBConfiguration.Add(config);
            }//select the configuration if only one configuration exists
            else if (ORMACConfiguration.Instance.DBConfiguration.Count == 1)
            {
                config = ORMACConfiguration.Instance.DBConfiguration.First();
            }//TODO: handle multiple databases
            else
            {
            }
            wizardStep = 0;
        }
        private void BuildImportWizard(){
            forms = new List<IWizardForm>();
            SQLConnectionForm SQLForm = new SQLConnectionForm(new SQLConnectionViewModel { OrmacConfig = config,BackActive=false });
            forms.Add(SQLForm);
            BasicSettingsForm basicSettingsForm = new BasicSettingsForm(new BasicSettingsViewModel { OrmacConfig = config });
            forms.Add(basicSettingsForm);
            SelectObjectsForm selectTablesForm = new SelectObjectsForm(new SelectTablesViewModel { OrmacConfig = config });
            forms.Add(selectTablesForm);
            SelectObjectsForm selectSPForm = new SelectObjectsForm(new SelectStoredProceduresViewModel { OrmacConfig = config,FinishActive=true,NextActive=false });
            forms.Add(selectSPForm);
        }
        /// <summary>
        /// Returns a list of files to be added to the project
        /// </summary>
        /// <returns></returns>
        public List<string> Run()
        {
            Init();
            List<string> res = new List<string>();
            WizardFormResult formResult = WizardFormResult.Next;
            while (formResult != WizardFormResult.Abort && formResult != WizardFormResult.Finish && formResult!=WizardFormResult.None)
            {
                BuildImportWizard();
                formResult = forms[wizardStep].ShowWizardPage();
                if (formResult == WizardFormResult.Next)
                    wizardStep++;
                if (formResult == WizardFormResult.Back)
                    wizardStep--;
            }
            if (formResult == WizardFormResult.Finish)
            {
                //generate temporary files for each class and pass the list of files to the caller
                res = config.GenerateObjectFiles();
                res.Add(GetConfigFile());
            }
            return res;
        }
        /// <summary>
        /// Returns the name of the ORMAC configuration file
        /// </summary>
        /// <returns>Configuration file name</returns>
        public string GetConfigFile()
        {
            return ORMACConfiguration.Instance.CreateConfigFile();
        }

    }
}
